<%@ page import="cz.tpj.logic.*" %>
<%@page import="cz.tpj.database.bod_zaujmu.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>

<%
response.setCharacterEncoding("UTF-8");
final String ACTION = "action";
final String ACTION_SHOW_SELECTED = "action_show";
final String ATTRACTIVE_PLACES = "selected_a_p";
final String MESSAGE = "message";
String message = (String)session.getAttribute(MESSAGE);
if(message == null)
	message = new String();
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="cz.tpj.database.aktivita_zajimavost.TableActivityInterests"%>
<%@page import="cz.tpj.util.ErrorDatabase"%>
<%@page import="java.util.StringTokenizer"%><html>
    <%= Navigation.getHead(null) %>
    <%= Navigation.getStartBody() %>
    <%= Navigation.getNavigationPanel(null, request) %>
          
        <div id="<%= Navigation.DIV_CENTER %>">
            <h1>Bod zájmu</h1>
            <p><%= message %></p>
            
          	<form method="POST" action="<%= Navigation.JSP_ATTRACTIVE_PLACE%>">
	          	<select name="<%=ATTRACTIVE_PLACES %>">
		          	<%
		          	//------------------najde vsecky zajimavosti-----------------------
		          	ArrayList<Bod_zaujmu> all_atractives = Table_Bod_zaujmu.getInstance().getAllInterests();
		          	Iterator<Bod_zaujmu> it = all_atractives.iterator();
		          	//pomocna promenna
		          	Bod_zaujmu attractive_place = null;
		          	//nacti posledne zvolenou zajimavost
		        	String selectedId = request.getParameter(ATTRACTIVE_PLACES);
		          	//projde vsecky zajmavosti a zapise jejich nazev do ComboBoxu
		          	while(it.hasNext()){
		          		attractive_place = it.next();
		          		String actualId = attractive_place.getId();
		          		if(actualId.equals(selectedId)){
		          			out.println("<option selected value='" + attractive_place.getId() + "'>" + attractive_place.getNazov() + "</option> ");
		          		}else{
		          			out.println("<option value='" + attractive_place.getId() + "'>" + attractive_place.getNazov() + "</option> ");
		          		}
		          	}
		          	%>
	          	</select>
	          	
	          	<input type="submit" name="<%= ACTION %>" value="Zobrazit">
          	</form>
          	<hr width="470" />
          	
          	<%
          	//akce podle zvoleneho tlacitka
          	String action = request.getParameter(ACTION);
          	//vybrane mesto v comboBoxu
          	String interestId = request.getParameter(ATTRACTIVE_PLACES);
          	//ukaz jestlize je zmacknuto tlacitko zobrezit a je vybrano alespon jedno mesto
          	if(action != null && interestId != null){
          		try{
	          		Bod_zaujmu b = Table_Bod_zaujmu.getInstance().getInterest(interestId);
	          		out.println("<p>" + b.getNazov() + "</p>");
	          		out.println("<p>" + b.getKratkyPopis() + "</p>");
	          		out.println("<p>" + b.getDlhyPopis() + "</p>");
	          		String gps = b.getGps();
	          		
	          		//nahradí znaky kvuli pozdejsi interpretaci prohizece (jedna se o uvozovky)
	          		gps = gps.replace("'","%27");
	          		gps = gps.replace("\"","%22");
	          		//vygeneruj odkaz
	          		out.println("<p><a href=\"http://www.mapy.cz/#x=135462912@y=134545408@z=6@mm=ZP@sa=s@st=s@ssq="+gps+"@sss=1@ssp=125714432_127524864_140525568_140804096\" target='_blank'>GPS zde</a></p>");
          		}catch(ErrorDatabase e){
          			session.setAttribute(MESSAGE, e.getMessage());
          		}
          		//najdi aktivity prirazene tomuto mistu a zobraz je
          		try{
              		String[] activities = TableActivityInterests.getInstance().getActivitiesOfInterestPlace(interestId);
              		if(activities.length > 0){
              			out.println("<h3>Aktivity:</h3>");
              			for(int i=0; i<activities.length; i++){
              				out.println("<p>" + activities[i] + "</p>");
              			}
              		}
          		}catch(ErrorDatabase e){
          			out.println("<p>Chyba při načítání aktivit přiřazených tomuto bodu zájmu.</p>");
          		}
          	}
          	
          	%>
          	
        </div>
 	
 	<%= Navigation.getLoginLink(request) %>
</html>